//
//  WYPersonWebHomePageController.m
//  Boss
//
//  Created by admin on 2018/7/26.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYPersonWebHomePageController.h"
#import <JavaScriptCore/JSContext.h>
#import <JavaScriptCore/JavaScriptCore.h>
@protocol JSObjcDelegate <JSExport>
// AndroidWebView对象调用的JavaScript方法，必须声明！！！
-(NSString *)waitJS;

@end
@interface WYPersonWebHomePageController ()<UIWebViewDelegate,JSObjcDelegate>
@property (nonatomic,strong) UIWebView *webView;
@end

@implementation WYPersonWebHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
   
    [self setupUI];
    // Do any additional setup after loading the view.
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer seeUerHomePage:self.urlString SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:response[@"result"]]]];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
    }];
    
    
    
}
-(void)returnToTop
{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)setupUI
{

    self.navigationItem.title = @"个人主页";
    self.webView = [[UIWebView alloc]init];
    [self.view addSubview:self.webView];
     self.webView.delegate = self;
    self.webView.frame = CGRectMake(0,self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"通用_返回白"] style:(UIBarButtonItemStylePlain) target:self action:@selector(returnToTop)];
    //self.webView.backgroundColor = [UIColor redColor];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // context = self;
    WEAKSELF
    context [@"ios"] = self;
    context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
        
        context[@"waitJS"]=  ^(NSString *name) {
            return [weakSelf waitJS];
        };
    };
}


-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // context = self;
    WEAKSELF
    context [@"ios"] = self;
    context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
        
        context[@"waitJS"]=  ^(NSString *name) {
            return [weakSelf waitJS];
        };
    };
}
-(NSString *)waitJS
{
    return @"iOS";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
